/**
 * Created by zhangzhihao on 2016/12/06 11:00;
 */
var vm = new Vue({
    el: '#main',
    data: {
        branchNodes: [],
        zTree: {},
        setting: {},
        user:{
        	optNo:App.getParam("optNo")
        },
        userBranchs:{
			datas:[],
			object:{
				optNo:App.getParam("optNo")
			}
		}
    },
    created: function () {
        this.setting = {
            check: {
                enable: true
            },
            data: {
            	simpleData: {
                    enable: true,
                    idKey: "branchId",
                    pIdKey: "branchParentId"
                },
                key: {
                    name: "branchName"
                }
            }/*,
            async: {    
                enable: true,    
                url:App.getApiUrl("getUserBranchAuthURL") + "?optNo=" + this.user.optNo,   
                autoParam:["branchId"] 
            } , callback: {
				 
				onAsyncSuccess: this.zTreeOnAsyncSuccess,//异步加载成功的fun    
				onAsyncError: this.zTreeOnAsyncError   //加载错误的fun    
			}*/
        };
        
        this.getUserInfo();
        this.getBranchTree();
        
    },
    methods: {
    	getUserInfo:function(){
    		App.ajax({
    			type:"GET",
    			url:App.getApiUrl("getUserDetailURL") + "?optNo=" + this.user.optNo,
    			success:function(data){
    				vm.user = data;
                    selectSet("optType",data.optType);
    			}
    		});
        },
        getBranchTree:function(){
        	App.ajax({
    			type:"GET",
    			url:App.getApiUrl("getUserBranchAuthURL") + "?optNo=" + this.user.optNo,
    			success:function(data){
    				vm.branchNodes = data;

                    $.fn.zTree.init($("#branchTree"), vm.setting, vm.branchNodes);
                    vm.zTree = $.fn.zTree.getZTreeObj("branchTree");
    			}
    		});
        },
        //返回上一页的点击事件的函数
        goBtn: function () {
            //不保留
            history.back(-1);
        },
        saveUserBranch:function(){
        	this.userBranchs.datas = [];
        	var nodes = this.zTree.getCheckedNodes(true);
        	for(var i=0;i<nodes.length;i++){
        		var halfCheck = 0;
        	 if(nodes[i].check_Child_State == 1){
        			halfCheck = 1;
        		} 
    			var parentNode = nodes[i].getParentNode();
    			var  childNodelenth = nodes[i].children;
    			 
    			 if(!parentNode || parentNode.check_Child_State == 1){
    				this.userBranchs.datas.push({
            			dataAuthNo:nodes[i].branchId,
            			isHalfCheck:halfCheck
            		});
    			} 
    			
    			 
    		/*	if((!parentNode || parentNode.check_Child_State == 1)&&(nodes[i].check_Child_State == 2||childNodelenth==undefined)){
    				this.userBranchs.datas.push({
            			dataAuthNo:nodes[i].branchId,
            			isHalfCheck:halfCheck
            		});
    			}*/
    			
    			
        	}
        	
        	App.ajax({
    			type:"POST",
    			url:App.getApiUrl("addUserDataAuthURL"),
    			data: this.userBranchs,
    			success:function(data){
    				parent.showAlert(data.msg);
    			}
    		});
    	},
    	zTreeOnAsyncError: function (event, treeId, treeNode){    
            alert("异步加载失败!");    
        },   
		zTreeOnAsyncSuccess: function zTreeOnAsyncSuccess(event, treeId, treeNode, msg){    
			 
			/*treeNode.icon = "";*/
			vm.zTree.updateNode(treeNode,true);
     }
    }
});